package com.lily.aidouzixun.datacenter;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.support.v4.util.ArrayMap;
import android.util.SparseArray;

import com.lily.aidouzixun.message.MessageDispatcher;
import com.lily.aidouzixun.message.MessageType;
import com.lily.aidouzixun.util.L;
import com.lily.aidouzixun.util.MyConstant;
import com.lily.aidouzixun.util.MyCrashHandler;
import com.lily.aidouzixun.util.Validator;

/**
 * 数据请求操作类
 */
public class DataReuireOperate {
    private static final String TAG = "DataReuireOperate";
    private ArrayMap<String, Object> params;    //请求参数
    private SparseArray<Object> results;   //请求结果  result[0]-key;result[1]-result
    private Handler appHandler; //Application中的Handler对象
    private int dataType;   //数据类型
    private Context appContext;

    public DataReuireOperate(Context context, Handler appHandler, ArrayMap<String, Object> params) {
        results = new SparseArray<Object>();
        this.appContext = context;
        this.appHandler = appHandler;
        this.params = params;
    }

    /**
     * 真正的数据请求
     *
     * @param asyncTask 异步请求的任务
     */
    public void requireOprate(AsyncTask<Object, Object, Object> asyncTask) {
        L.i(TAG, "requireOprate!");
        if (params == null) {
            return;
        }

        String key = (String) params.get(MyConstant.RequireParams.MAP_KEY);  //
        dataType = (Integer) params.get(MyConstant.RequireParams.DATA_TYPE);
        if (Validator.isEffective(key)) {
            results.put(0, key);
            DataProvider dataProvider = DataProvider.getInstance();
            switch (dataType) {
                case MyConstant.DataType.SEND_CRASH_LOG:     //发送CrashLog
                    if (params != null) {
                        MyCrashHandler.getInstance().sendPreviousReportsToServer();
                        params.clear();
                    }
                    break;
                case MyConstant.DataType.USERCENTER_INFO:   //用户信息
                    String urid = (String) params.get(MyConstant.RequireParams.URID);
                    SparseArray<Object> userinfo = dataProvider.getUserInfo(this.appContext, urid);
                    results.put(1, userinfo);
                    params.clear();
                    break;
                case MyConstant.DataType.USER_LOGIN:    //用户登录  results(0)-key;result(1)-result[2]
                    String loginName = (String) params.get(MyConstant.RequireParams.USER_NAME);
                    String loginPwd = (String) params.get(MyConstant.RequireParams.PASSWORD);
                    SparseArray<Object> result = dataProvider.userLogin(this.appContext, loginName, loginPwd);
                    results.put(1, result);
                    params.clear();
                    break;
                case MyConstant.DataType.USER_REGISTER:  //  用户注册
                    String registerName = (String) params.get(MyConstant.RequireParams.USER_NAME);
                    String registerPwd = (String) params.get(MyConstant.RequireParams.PASSWORD);
                    SparseArray<Object> register = dataProvider.userRegister(this.appContext, registerName, registerPwd);
                    results.put(1, register);
                    params.clear();
                    break;
                case MyConstant.DataType.MODIFY_USERINFO:  //  修改用户信息
                    int modifyAction = (int) params.get(MyConstant.RequireParams.ACTION);
                    String modifyContent = (String) params.get(MyConstant.RequireParams.MODIFY_USERINFO);
                    SparseArray<Object> modifyReslut = dataProvider.modifyUserInfo(this.appContext, modifyAction, modifyContent);
                    results.put(1, modifyReslut);
                    params.clear();
                    break;
                case MyConstant.DataType.UPLOAD_AVATAR:  //  上传头像
                    String avatarPath = (String) params.get(MyConstant.RequireParams.PHOTO_PATH);
                    SparseArray<Object> uploadAvatarResult = dataProvider.uploadAvatar(this.appContext, avatarPath);
                    results.put(1, uploadAvatarResult);
                    params.clear();
                    break;
                case MyConstant.DataType.MODIFY_PASSWORD:  //  修改用户密码
                    String username = (String) params.get(MyConstant.RequireParams.USER_NAME);
                    String oldPwd = (String) params.get(MyConstant.RequireParams.PASSWORD);
                    String newPwd = (String) params.get(MyConstant.RequireParams.NEW_PASSWORD);
                    SparseArray<Object> modifyPwdReslut = dataProvider.modifyPassword(this.appContext, username, oldPwd, newPwd);
                    results.put(1, modifyPwdReslut);
                    params.clear();
                    break;
                case MyConstant.DataType.FEEDBACK:  //  意见反馈
                    String feedback = (String) params.get(MyConstant.RequireParams.FEEDBACK);
                    SparseArray<Object> feedbackReslut = dataProvider.userFeedback(this.appContext, feedback);
                    results.put(1, feedbackReslut);
                    params.clear();
                    break;
                case MyConstant.DataType.LONGDISTANCE_HOME:  //文章首页
                    int longdistanceHomePage = (int) params.get(MyConstant.RequireParams.PAGE);
                    SparseArray<Object> longdistanceHome = dataProvider.getLongDistanceHome(this.appContext, longdistanceHomePage);
                    results.put(1, longdistanceHome);
                    params.clear();
                    break;
                case MyConstant.DataType.DISCUSS_HOME:  //  动态首页
                    int discussCategory = (int) params.get(MyConstant.RequireParams.CATEGORY);
                    int pager = (int) params.get(MyConstant.RequireParams.PAGE);
                    SparseArray<Object> discussHome = dataProvider.getDiscussHome(this.appContext, discussCategory, pager);
                    results.put(1, discussHome);
                    params.clear();
                    break;
                case MyConstant.DataType.DISCUSS_MINE:  //  我发表的动态信息
                    int discussMinePage = (int) params.get(MyConstant.RequireParams.PAGE);
                    SparseArray<Object> discussMineResult = dataProvider.getDiscussHome(this.appContext, 2, discussMinePage);
                    results.put(1, discussMineResult);
                    params.clear();
                    break;
                case MyConstant.DataType.DISCUSS_COLLECT:  //  动态收藏
                    int discussCollectPager = (int) params.get(MyConstant.RequireParams.PAGE);
                    SparseArray<Object> discussCollect = dataProvider.getDiscussCollect(this.appContext, discussCollectPager);
                    results.put(1, discussCollect);
                    params.clear();
                    break;
                case MyConstant.DataType.PUBLISH_DISCUSS_TEXT:  //  发布动态文字
                    String discussContent = (String) params.get(MyConstant.RequireParams.DISCUSS_CONTENT);
                    int hasImage = (int) params.get(MyConstant.RequireParams.HAS_IMAGE);
                    SparseArray<Object> publishDiscussTextResult = dataProvider.publishDiscussText(this.appContext, discussContent, hasImage);
                    results.put(1, publishDiscussTextResult);
                    params.clear();
                    break;
                case MyConstant.DataType.PUBLISH_DISCUSS_PHOTO:  //  发布动态照片
                    String imageName = (String) params.get(MyConstant.RequireParams.IMAGE_NAME);
                    String photoPath = (String) params.get(MyConstant.RequireParams.PHOTO_PATH);
                    SparseArray<Object> publishDiscussPhotoResult = dataProvider.publishDiscussPhoto(this.appContext, imageName, photoPath);
                    results.put(1, publishDiscussPhotoResult);
                    params.clear();
                    break;
                case MyConstant.DataType.DELETE_DISCUSS:  //  删除动态
                    String deleteDid = (String) params.get(MyConstant.RequireParams.DID);
                    SparseArray<Object> deleteDiscussResult = dataProvider.deleteDiscuss(this.appContext, deleteDid);
                    results.put(1, deleteDiscussResult);
                    params.clear();
                    break;
                case MyConstant.DataType.DISCUSS_COMMENT:  //  提交评论
                    String did = (String) params.get(MyConstant.RequireParams.DID);
                    String discussComment = (String) params.get(MyConstant.RequireParams.COMMENT);
                    SparseArray<Object> discussCommentReslut = dataProvider.userDiscussComment(this.appContext, did, discussComment);
                    results.put(1, discussCommentReslut);
                    params.clear();
                    break;
                case MyConstant.DataType.COMMENT_DATA:  //  得到评论数据
                    int commentPager = (int) params.get(MyConstant.RequireParams.PAGE);
                    String disscussID = (String) params.get(MyConstant.RequireParams.DID);
                    SparseArray<Object> commentDataReslut = dataProvider.commentData(this.appContext, disscussID, commentPager);
                    results.put(1, commentDataReslut);
                    params.clear();
                    break;
                case MyConstant.DataType.SUPPORT_DISCUSS:  //  动态点赞
                    String supportDID = (String) params.get(MyConstant.RequireParams.DID);
                    int supportAction = (int) params.get(MyConstant.RequireParams.ACTION);
                    SparseArray<Object> supportReslut = dataProvider.supportDiscuss(this.appContext, supportDID, supportAction);
                    results.put(1, supportReslut);
                    params.clear();
                    break;
                case MyConstant.DataType.COLLECT_DISCUSS:  //  动态收藏
                    String collectDID = (String) params.get(MyConstant.RequireParams.DID);
                    int collectAction = (int) params.get(MyConstant.RequireParams.ACTION);
                    SparseArray<Object> collectReslut = dataProvider.collectDiscuss(this.appContext, collectDID, collectAction);
                    results.put(1, collectReslut);
                    params.clear();
                    break;
                case MyConstant.DataType.OPPOSITION_DISCUSS:  //  反对动态
                    String oppositionDID = (String) params.get(MyConstant.RequireParams.DID);
                    SparseArray<Object> oppositionReslut = dataProvider.oppositionDiscuss(this.appContext, oppositionDID);
                    results.put(1, oppositionReslut);
                    params.clear();
                    break;
                case MyConstant.DataType.LONGDISTANCE_COMMENT:  //  文章评论
                    String ldid = (String) params.get(MyConstant.RequireParams.LDID);
                    String longdistanceComment = (String) params.get(MyConstant.RequireParams.COMMENT);
                    SparseArray<Object> longdistanceCommentReslut = dataProvider.longDistanceComment(this.appContext, ldid, longdistanceComment);
                    results.put(1, longdistanceCommentReslut);
                    params.clear();
                    break;
                case MyConstant.DataType.BIND_QQ:  //  绑定QQ
                    String openid = (String) params.get(MyConstant.RequireParams.QQ_ID);
                    String nickName = (String) params.get(MyConstant.RequireParams.NICKNAME);
                    String avatar = (String) params.get(MyConstant.RequireParams.AVATAR);
                    SparseArray<Object> bindQQResult = dataProvider.bindQQ(this.appContext, openid, nickName, avatar);
                    results.put(1, bindQQResult);
                    params.clear();
                    break;
                case MyConstant.DataType.BIND_PHONE:  //  绑定手机号
                    String bindPhone = (String) params.get(MyConstant.RequireParams.PHONE);
                    SparseArray<Object> bindPhoneResult = dataProvider.bindPhone(this.appContext, bindPhone);
                    results.put(1, bindPhoneResult);
                    params.clear();
                    break;
                case MyConstant.DataType.USER_SIGN:  //  用户签到
                    SparseArray<Object> signResult = dataProvider.userSign(this.appContext);
                    results.put(1, signResult);
                    params.clear();
                    break;
                default:
                    break;
            }
        } else {
            L.e(TAG, "mapkey is null!");
        }
        if (asyncTask.isCancelled() == false) {  //如果当前任务没有被取消，则发消息给Application中的handler,通知数据请求完成
            MessageDispatcher.sendMessage(appHandler, MessageType.REQUIRE_DATA_FINISHED, dataType, results);
        }
        appHandler = null;
    }
}
